package controllers;

import java.util.Date;
import java.util.List;
import java.util.Random;

import com.google.gson.JsonObject;

import models.Idea;
import models.Notification;
import models.Organization;
import models.OrganizationLead;
import models.Request;
import models.TopicRelation;
import play.mvc.Controller;
import play.mvc.With;
import models.IdeaDeveloper;

@With(Secure.class)
public class IdeaDeveloperFunctions extends Controller {
	static Random generator = new Random();

	/**
	 * renders the show req form page
	 */
	public static void showReqForm() {
		Long loggedInUserID = Security.userid();
		String loggedInUserName = Security.connected();

		render(loggedInUserID, loggedInUserName);

	}

	/**
	 * allows the idea developer to request to join organization
	 * 
	 * @param organizationName
	 *            organization name where request is sent
	 * @param devoloperusername
	 *            idea developer sending request
	 */
	public static void requestJoinOrganization(String organizationName,
			String devoloperusername) {
		Organization organization = Organization.find("name=?",
				organizationName).first();
		IdeaDeveloper devoloper = IdeaDeveloper.find("username=?",
				devoloperusername).first();
		Request.RequestMe(organization, devoloper, "JoinOrgFromDevToOrg");
		JsonObject requestResponse = new JsonObject();
		requestResponse.addProperty("res", "Request sent !");
		Notification.notify(" has requested to join the organization <a href='/application/organizationview?name="
                        + organization.name
                        + "'>"
                        + organization.name
                        + "</a>", organization.creator.ideaDeveloper, devoloper, new Date());
		renderJSON(requestResponse.toString());
	}

	/**
	 * join public Organization and get accepted automatically
	 * 
	 * @param orgId the organization that we want to request
	 * @param ideaDevoloperId the id of developer that want to join
         * Join is wrong in the code but i cannot change it because i will need to loop over alot of things
	 */
	public static void JoinPublicOrganization(Long orgId, Long ideaDevoloperId) {
		Request.publicOrganizationRequest(orgId, ideaDevoloperId);
	}

	
	/**
         * @author tarek
         * this method is used to go crazy which switches to different ideas randomly
         * @param type the type of the idea
         */
	public static void reshuffle(String type) {
		JsonObject reshuffleResponse = new JsonObject();

		if (type.equalsIgnoreCase("idea")) {

			List ideas = Idea.findAll();

			int randomIndex = generator.nextInt(ideas.size());

			while (true) {

				if (ideas.get(randomIndex) != null) {
					Idea ide = (Idea) ideas.get(randomIndex);
					reshuffleResponse.addProperty("res", ide.id);
					renderJSON(reshuffleResponse.toString());
					break;
				}
				randomIndex = generator.nextInt(ideas.size());

			}

		}

	}
}
